#!/bin/sh
#
# This is the script for generating files for a specific Dalton test job.
#
# For the .check file ksh or bash is preferred, otherwise use sh
# (and hope it is not the old Bourne shell, which will not work)
#
if [ -x /bin/ksh ]; then
   CHECK_SHELL='#!/bin/ksh'
elif [ -x /bin/bash ]; then
   CHECK_SHELL='#!/bin/bash'
else
   CHECK_SHELL='#!/bin/sh'
fi


#######################################################################
#  TEST DESCRIPTION
#######################################################################
cat > geoopt_redintmin.info <<'%EOF%'
   geoopt_redintmin
   ----------------
   Molecule:         C2H6
   Wave Function:    SCF / STO-3G
   Test Purpose:     Minimization in redundant internal coordinates using
                     a model Hessian. The BFGS-formula is used for updating
                     the Hessians
%EOF%

#######################################################################
#  MOLECULE INPUT
#######################################################################
cat > geoopt_redintmin.mol <<'%EOF%'
BASIS
STO-3G
First order minimization in redundant internal coordinates

    2
        6.    2
C     0.0000000000        0.0000000000        1.4547876282             *
C     0.0000000000        0.0000000000       -1.4547876282             *
        1.    6
H     1.6808445513        0.9704360937        2.1445545504             *
H    -1.6808445513        0.9704360937        2.1445545504             *
H     0.0000000000       -1.9408721894        2.1445545504             *
H     1.6808445513       -0.9704360937       -2.1445545504             *
H    -1.6808445513       -0.9704360937       -2.1445545504             *
H     0.0000000000        1.9408721894       -2.1445545504             *
%EOF%

#######################################################################
#  DALTON INPUT
#######################################################################
cat > geoopt_redintmin.dal <<'%EOF%'
**DALTON INPUT
.OPTIMIZE
*OPTIMIZE
.REDINT
.INIMOD
**WAVE FUNCTION
.HF
**END OF DALTON INPUT
%EOF%
#######################################################################



#######################################################################

#######################################################################
#  CHECK SCRIPT
#######################################################################
echo $CHECK_SHELL >geoopt_redintmin.check
cat >>geoopt_redintmin.check <<'%EOF%'
log=$1

if [ `uname` = Linux ]; then
   GREP="egrep -a"
else
   GREP="egrep"
fi

# Geometry optimization
CRIT1=`$GREP "Default 1st order method will be used\: * BFGS update\." $log | wc -l`
CRIT2=`$GREP "Optimization will be performed in redundant internal coordinates\." $log | wc -l`
CRIT3=`$GREP "Model Hessian will be used as initial Hessian\." $log | wc -l`
CRIT4=`$GREP "Trust region method will be used to control step \(default\)\." $log | wc -l`
TEST[1]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4`
CTRL[1]=4
ERROR[1]="GEOMETRY OPTIMIZATION NOT SET UP CORRECTLY"

# Symmetry
# Aug.18: removing tests on how molecule is rotated, are not important
#CRIT1=`$GREP "Symmetry point group found\: D\(3d\)" $log | wc -l`
#CRIT2=`$GREP "The following symmetry elements were found\: * Y * XZ" $log | wc -l`
CRIT3=`$GREP "Full point group is\: * D\(3d\)" $log | wc -l`
CRIT4=`$GREP "Represented as\: * C2h" $log | wc -l`
#CRIT5=`$GREP "Reflection in the xz\-plane" $log | wc -l`
#CRIT6=`$GREP "Rotation about the y\-axis" $log | wc -l`
CRIT7=`$GREP "Number of coordinates in each symmetry\: * 7 * 5 * 7 * 5" $log | wc -l`
CRIT8=`$GREP "Number of orbitals in each symmetry\: * 6 * 2 * 6 * 2" $log | wc -l`
#TEST[2]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4 \+ $CRIT5 \+ $CRIT6 \+ \
#		$CRIT7 \+ $CRIT8`
#CTRL[2]=10
TEST[2]=`expr	 $CRIT3 \+ $CRIT4 \+ $CRIT7 \+ $CRIT8`
CTRL[2]=4
ERROR[2]="SYMMETRY NOT CORRECT"

# Geometry (removed sign check because depends on order of symops /hjaaj)
CRIT1=`$GREP "Total number of coordinates\: * 24" $log | wc -l`
CRIT2=`$GREP "C .* .[0-9] *x .*1\.454787" $log | wc -l`
CRIT3=`$GREP "H .* .[0-9] *x .*0*\.970436" $log | wc -l`
CRIT4=`$GREP "H .* .[0-9] *x .*1\.680844" $log | wc -l`
CRIT5=`$GREP "H .* .[0-9] *x .*1\.940872" $log | wc -l`
CRIT6=`$GREP "H .* .[0-9] *x .*2\.144554" $log | wc -l`
TEST[3]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4 \+ $CRIT5 \+ $CRIT6`
CTRL[3]=19
ERROR[3]="GEOMETRY NOT READ CORRECTLY"

# Initial energy
CRIT1=`$GREP "Final * HF energy\: * \-78\.3055756[1-2]...[0-9]" $log | wc -l`
TEST[4]=`expr	$CRIT1`
CTRL[4]=1
ERROR[4]="INITIAL ENERGY NOT CORRECT"

# Initial gradient
CRIT1=`$GREP "C .*   0*\.0098(2|3)" $log | wc -l`
CRIT2=`$GREP -l "C .*   0*\.00000" $log | wc -l`
CRIT3=`$GREP "H .* \-0*\.00516" $log | wc -l`
CRIT4=`$GREP "H .*   0*\.01013" $log | wc -l`
CRIT5=`$GREP "H .* \-0*\.01033" $log | wc -l`
CRIT6=`$GREP "H .* \-0*\.01013" $log | wc -l`
CRIT7=`$GREP "H .*   0*\.0175(5|6)" $log | wc -l`
TEST[5]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4 \+ $CRIT5 \+ $CRIT6 \+ \
		$CRIT7`
CTRL[5]=7
ERROR[5]="INITIAL GRADIENT NOT CORRECT"

# Initial step
TEST[6]=`$GREP "Norm of step .* 0\.165(39|40)" $log | wc -l`
CTRL[6]=1
ERROR[6]="INITIAL STEP NOT CORRECT"

# Second iteration
CRIT1=`$GREP "Energy at this geometry is * \: * \-78\.30611[0-9]" $log | wc -l`
CRIT2=`$GREP "Norm of gradient * \: * (0| )\.00383[0-9]" $log | wc -l`
CRIT3=`$GREP "Norm of step * \: * (0| )\.04519[0-9]" $log | wc -l`
TEST[7]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3`
CTRL[7]=3
ERROR[7]="SECOND ITERATION NOT CORRECT"

# Third iteration
CRIT1=`$GREP "Norm of gradient * \: * (0| )\.00037[0-9]" $log | wc -l`
CRIT2=`$GREP "Norm of step * \: * (0| )\.00236[0-9]" $log | wc -l`
TEST[8]=`expr	$CRIT1 \+ $CRIT2`
CTRL[8]=2
ERROR[8]="THIRD ITERATION NOT CORRECT"

# Final geometry, now checked with nuclear repulsion /hjaaj Nov 2007
# geo 4  Final HF energy:   -78.306179650041
# geo 4  Nuclear repulsion:  42.274073987153
# geo 5  Final HF energy:   -78.306179650658 change 6.D-8
# geo 5  Nuclear repulsion:  42.274202197306 change 2.d-4, NB! much more sensitive!

TEST[9]=`$GREP "Nuclear repulsion\: * 42\.2740[67]" $log | wc -l`
CTRL[9]=1
ERROR[9]="FINAL GEOMETRY NOT CORRECT"

# Geometry converged
CRIT1=`$GREP "Geometry converged in * 4  *iterations" $log | wc -l`
CRIT2=`$GREP "Energy at final geometry is * \: * \-78\.306180 a\.u\." $log | wc -l`
CRIT3=`$GREP "Energy change during optimization \: * \-0*\.000604 a\.u\." $log | wc -l`
TEST[10]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3`
CTRL[10]=3
ERROR[10]="GEOMETRY OPTIMIZATION NOT CONVERGED CORRECTLY"

PASSED=1
for i in 1 2 3 4 5 6 7 8 9 10
do
   if [ ${TEST[i]} -ne ${CTRL[i]} ]; then
     echo "${ERROR[i]} ( test = ${TEST[i]}; control = ${CTRL[i]} )"
     PASSED=0
   fi
done

if [ $PASSED -eq 1 ]
then
  echo TEST ENDED PROPERLY
  exit 0
else
  echo THERE IS A PROBLEM
  exit 1
fi

%EOF%
#######################################################################
